// 
// Entify project
//  
// Author:
//       Lauri Taimila <lauri@taimila.com>
// 
// Copyright (c) 2010 Lauri Taimila
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Reflection;

namespace Taimila.Entify.Attributes
{
	/// <summary>
	/// Checks that value is within the specified range.
	/// </summary>
	[AttributeUsage(AttributeTargets.Property)]
	public sealed class NumericRangeValidatorAttribute : ValidatorAttribute
	{
		/// <summary>
		/// Bounds as long integers.
		/// </summary>
		private long iLowerBound, iUpperBound;
		
		/// <summary>
		/// Bounds as doubles.
		/// </summary>
		private double dLowerBound, dUpperBound;
		
		/// <summary>
		/// Initialize new range validator for int property.
		/// </summary>
		/// <param name="lowerBound">
		/// Lower bound. <see cref="System.Int64"/>
		/// </param>
		/// <param name="upperBound">
		/// Upper bound. <see cref="System.Int64"/>
		/// </param>
		public NumericRangeValidatorAttribute(long lowerBound, long upperBound)
		{
			this.iLowerBound = lowerBound;
			this.iUpperBound = upperBound;
		}
		
		/// <summary>
		/// Initialize new range validator for int property.
		/// </summary>
		/// <param name="lowerBound">
		/// Lower bound. <see cref="System.Double"/>
		/// </param>
		/// <param name="upperBound">
		/// Upper bound. <see cref="System.Double"/>
		/// </param>
		public NumericRangeValidatorAttribute(double lowerBound, double upperBound)
		{
			this.dLowerBound = lowerBound;
			this.dUpperBound = upperBound;
		}
		
		/// <summary>
		/// Tests is specified value within the specified range.
		/// </summary>
		/// <param name="property">
		/// Property which value is validated.
		/// </param>
		/// <param name="value">
		/// Value to be validated. <see cref="System.Object"/>
		/// </param>
		/// <returns>
		/// ValidationError or null if valid. <see cref="ValidationError"/>
		/// </returns>
		public override ValidationError ValidateValue(PropertyInfo property, object value)
		{
			if(value is double || value is float || value is decimal)
			{
				if(Convert.ToDouble(value) < this.dLowerBound)
				{
					return new ValidationError(ValidationErrorReason.TooSmallValue, property, property.Name + " is not within the specified range.");
				}
				
				if(Convert.ToDouble(value) > this.dUpperBound)
				{
					return new ValidationError(ValidationErrorReason.TooLargeValue, property, property.Name + " is not within the specified range.");
				}
			}
			else
			{
				if(Convert.ToInt64(value)  < this.iLowerBound)
				{
					return new ValidationError(ValidationErrorReason.TooSmallValue, property, property.Name + " is not within the specified range.");
				}
				
				if(Convert.ToInt64(value) > this.iUpperBound)
				{
					return new ValidationError(ValidationErrorReason.TooLargeValue, property, property.Name + " is not within the specified range.");
				}
			}
			
			return null;
		}
	}
}
